/*
 * Classe com definição da interface e funções relacionados a lógica de
 * apresentação e controle (paradigma MVC)
 */

/*
 * Form.java
 *
 * Created on 29/05/2011, 15:40:33
 */

package calculodissidio;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

/**
 *
 * @author wagner
 */
public class Form extends javax.swing.JFrame {

    //objetos da interface definidos no final da classe
    //utiliza a classe Calcdissidio
    private CalcDissidio dissidio = new CalcDissidio();

    /** Creates new form Form */
    public Form() {
        /*
         * Inicia os componentes da interface com as configurações definidas
         * no modo de 'projeto' do netbeans, corresponde a apresentção (MVC)
         */         
        initComponents();
        
        /**
         * @prova No ComboBox devem existir as opções Auxiliar, Técnico, 
         * Supervisor e Gerente.
         */
        cmb_cargo.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "Auxiliar", "Tecnico", "Supervisor", "Gerente" }
        ));

        /**
         * @prova No TextField de entrada de nome, deve ser possível a entrada 
         * apenas de caracteres alfabéticos
         * 
         * definido um verificador que remove os numeros do campo nome
         */
        fld_nome.setInputVerifier(new InputVerifier() {
            @Override
            public boolean verify(JComponent input) {                
                fld_nome.setText(fld_nome.getText().replaceAll("[0-9]", ""));
                return true;
            }
        });
        
        /**
         * @prova No TextField de entrada de salário pode-se entrar
         * apenas números.
         *
         * define uma mascara para o campo salario, pertimindo apenas numeros
         */
        fld_salario.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(
                    new javax.swing.text.NumberFormatter(
                        new java.text.DecimalFormat("#,##0.00")
                      )
                )
        );
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        cmb_cargo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        fld_salario = new javax.swing.JFormattedTextField();
        bt_calcular = new javax.swing.JButton();
        bt_limpar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        area_resultado = new javax.swing.JTextArea();
        fld_nome = new javax.swing.JTextField();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cargos e Salários");

        jLabel1.setText("Escolha o cargo:");

        cmb_cargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Auxiliar", "Tecnico", "Supervisor", "Gerente" }));

        jLabel2.setText("Nome:");

        jLabel3.setText("Salário:");

        fld_salario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));

        bt_calcular.setText("Calcular");
        bt_calcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_calcularActionPerformed(evt);
            }
        });

        bt_limpar.setText("Limpar");
        bt_limpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_limparActionPerformed(evt);
            }
        });

        area_resultado.setColumns(20);
        area_resultado.setRows(5);
        jScrollPane1.setViewportView(area_resultado);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                        .addComponent(bt_limpar, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(fld_nome)
                            .addComponent(fld_salario)
                            .addComponent(cmb_cargo, 0, 254, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmb_cargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fld_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(fld_salario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_calcular)
                    .addComponent(bt_limpar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


	//bloco com os principais controles (MVC)
	//-----------------------
	
    /**
     * @prova O botão Limpar Dados deve limpar o conteúdo dos 2 TextFields e 
     * colocar o ComboBox na posição Auxiliar.
     * @param evt
     */
    //limpa os campos, ao clicar no botão limpar
    private void bt_limparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_limparActionPerformed
        fld_nome.setText(null);
        fld_salario.setText(null);
        cmb_cargo.setSelectedIndex(0);
    }//GEN-LAST:event_bt_limparActionPerformed

    /**
     * @prova Validação e calculo do dissidio
     * @param evt
     */
    private void bt_calcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_calcularActionPerformed
        int id_cargo = cmb_cargo.getSelectedIndex();
        String nome = fld_nome.getText();
        double salario = Double.parseDouble(fld_salario.getValue().toString());
        double aumento = 0;
        String saida= "";

        //caso o salario estiver fora da faixa salarial, será apresentado uma msg
        if(!dissidio.validaSalario(id_cargo, salario)){
            msgBoxFaixaSalarial(id_cargo);
        }else{
            //calcula dissidio
            aumento = dissidio.calculaDissidio(id_cargo, salario);
            saida += "\nNome: " + nome;
            saida += "\nCargo: " + dissidio.getCargo(id_cargo);
            saida += "\nSalário: " + salario;
            saida += "\nAumento de " + dissidio.getPercDissidio(id_cargo);
            saida += "\nTotal: " + (salario + aumento);
            saida += "\n------------------------\n";
            area_resultado.append(saida);
                    
        }

    }//GEN-LAST:event_bt_calcularActionPerformed

    //função auxiliar para mostrar a faixa salarial permitida
    private void msgBoxFaixaSalarial(int id_cargo){
        JOptionPane.showMessageDialog(this,
        dissidio.getFaixaSalarial(id_cargo),
        "Salário fora da faixa",
        JOptionPane.INFORMATION_MESSAGE);
	}
	
	//-----------------------

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea area_resultado;
    private javax.swing.JButton bt_calcular;
    private javax.swing.JButton bt_limpar;
    private javax.swing.JComboBox cmb_cargo;
    private javax.swing.JTextField fld_nome;
    private javax.swing.JFormattedTextField fld_salario;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


}
